package com.ruoyi.project.system.setting.service.impl;

import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.project.monitor.job.domain.Point3D;
import com.ruoyi.project.system.setting.domain.TbParamSetting;
import com.ruoyi.project.system.setting.mapper.TbParamSettingMapper;
import com.ruoyi.project.system.setting.service.ITbParamSettingService;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 基本参数设置Service业务层处理
 * 
 * @author kangjie
 * @date 2019-12-28
 */
@Service
public class TbParamSettingServiceImpl implements ITbParamSettingService 
{
    @Autowired
    private TbParamSettingMapper tbParamSettingMapper;

    /**
     * 查询基本参数设置
     * 
     * @param
     * @return 基本参数设置
     */
    @Override
    public TbParamSetting selectTbParamSettingById(String id)
    {
        return tbParamSettingMapper.selectTbParamSettingById( id);
    }

    /**
     * 查询基本参数设置列表
     * 
     * @param tbParamSetting 基本参数设置
     * @return 基本参数设置
     */
    @Override
    public List<TbParamSetting> selectTbParamSettingList(TbParamSetting tbParamSetting)
    {
        return tbParamSettingMapper.selectTbParamSettingList(tbParamSetting);
    }

    /**
     * 新增基本参数设置
     * 
     * @param tbParamSetting 基本参数设置
     * @return 结果
     */
    @Override
    public int insertTbParamSetting(TbParamSetting tbParamSetting)
    {
        return tbParamSettingMapper.insertTbParamSetting(tbParamSetting);
    }

    /**
     * 修改基本参数设置
     * 
     * @param tbParamSetting 基本参数设置
     * @return 结果
     */
    @Override
    public int updateTbParamSetting(TbParamSetting tbParamSetting)
    {
        return tbParamSettingMapper.updateTbParamSetting(tbParamSetting);
    }

    /**
     * 删除基本参数设置对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteTbParamSettingByIds(String ids)
    {
        return tbParamSettingMapper.deleteTbParamSettingByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除基本参数设置信息
     * 
     * @param
     * @return 结果
     */
    @Override
    public int deleteTbParamSettingById(String id)
    {
        return tbParamSettingMapper.deleteTbParamSettingById(id);
    }

    /**
     * 获取启用数据条数
     *
     */
    @Override
    public void checkFlag() {
        int count = tbParamSettingMapper.getTbParamSettingCount();
        if (count>1){
            throw new BusinessException("不允许启用数据大于1条");
        }
    }

    /**
     * 修改参数标识
     * @param tbParamSetting
     * @return
     */
    @Override
    public int updateFlag(TbParamSetting tbParamSetting) {

        return tbParamSettingMapper.updateFlag(tbParamSetting);
    }

    /**
     * 获取参数
     * @return
     */
    @Override
    public TbParamSetting getParam() {
        return tbParamSettingMapper.getParam();
    }

    /**
     * 获取400个单元格，0，1值;
     * @param list
     * @return
     */
    @Override
    public String autoDeal(List<Point3D> list) {
        TbParamSetting tbParamSetting = tbParamSettingMapper.getParam();
        int xwith = 185;//单元格长
        int ywith = 200;//单元格宽
        int H = tbParamSetting.gethCoordinateCount();//高度限制
        Map<String,Integer> mapCompare = getGrid();
        Map<String,Integer> map = getGrid();

        for (Point3D p: list) {
            int xg = (int) (Math.abs(p.x)/xwith);
            if ((Math.abs(p.x)%185)==0){
                xg = xg-1;
            }
            if (xg < 0){
                xg =0;
            }

            int yg = (int) (Math.abs(p.y)/ywith);
            if ((Math.abs(p.y)%200) == 0){
                yg = yg -1;
            }
            if (yg < 0){
                yg = 0;
            }
            if (Math.abs(p.z)>H){
                map.put("["+(xg)+","+(yg)+"]",1);
            }
        }
        Set<String> key = map.keySet();

        System.out.println("单元格个数："+key.size());
        StringBuffer stringBuffer = new StringBuffer();
        Set<String> set = mapCompare.keySet();
        for (String str : set){
            Integer value = map.get(str);
            if (value>0){

                stringBuffer.append("0");
            }else {

                stringBuffer.append("1");
            }
        }
        return stringBuffer.toString();
    }

    /**
     * 保存plc状态
     * @param
     * @param startStatus
     * @param model
     * @return
     */
    @Override
    public String savePlcStatus(String startStatus,String model,
    		String count,String error,
    		String is_complete) {

        int number = tbParamSettingMapper.savePlcStatus(startStatus,model,count,error,is_complete);
        if (number >0 ){
            return "model";
        }
        return "fail";
    }

    /**
     * 获取登录天数
     * @return
     */
    @Override
    public Map<String, Long> getLoginDayCount() {
        Map<String,Integer>  paramsMap = new HashMap<>();
        paramsMap.put("startDays",5);
        paramsMap.put("endDays",4);
        //5日前登录次数
        Map<String,Long> fiveDaysMap = tbParamSettingMapper.getLoginDayCount(paramsMap);
        paramsMap.put("startDays",4);
        paramsMap.put("endDays",3);
        //4日前登录次数
        Map<String,Long> fourDaysMap = tbParamSettingMapper.getLoginDayCount(paramsMap);

        paramsMap.put("startDays",3);
        paramsMap.put("endDays",2);
        //3日前登录次数
        Map<String,Long> threeDaysMap = tbParamSettingMapper.getLoginDayCount(paramsMap);

        paramsMap.put("startDays",2);
        paramsMap.put("endDays",1);
        //2日前登录次数
        Map<String,Long> twoDaysMap = tbParamSettingMapper.getLoginDayCount(paramsMap);

        paramsMap.put("startDays",1);
        paramsMap.put("endDays",0);
        //1日前登录次数
        Map<String,Long> firstDaysMap = tbParamSettingMapper.getLoginDayCount(paramsMap);

        //近5日登录总数
        paramsMap.put("startDays",5);
        paramsMap.put("endDays",0);
        Map<String,Long> sumLoginCount = tbParamSettingMapper.getLoginDayCount(paramsMap);

        Map<String,Long> resultMap = new HashMap<>();
        
        resultMap.put("fiveDaysMap",  fiveDaysMap.get("number"));
        resultMap.put("fourDaysMap",  fourDaysMap.get("number"));
        resultMap.put("threeDaysMap", threeDaysMap.get("number"));
        resultMap.put("twoDaysMap",   twoDaysMap.get("number"));
        resultMap.put("firstDaysMap", firstDaysMap.get("number"));
        resultMap.put("sumLoginCount",Long.valueOf(sumLoginCount.get("number")));

        return resultMap;
    }
    /**
     * 获取扫描次数
     * @return
     */
    @Override
    public Map<String, Long> getScanDayCount() {
        Map<String,Integer>  paramsMap = new HashMap<>();
        paramsMap.put("startDays",5);
        paramsMap.put("endDays",4);
        //5日前扫描次数
        Map<String,Long> fiveDaysMap = tbParamSettingMapper.getScanDayCount(paramsMap);
        paramsMap.put("startDays",4);
        paramsMap.put("endDays",3);
        //4日前扫描次数
        Map<String,Long> fourDaysMap = tbParamSettingMapper.getScanDayCount(paramsMap);

        paramsMap.put("startDays",3);
        paramsMap.put("endDays",2);
        //3日前扫描次数
        Map<String,Long> threeDaysMap = tbParamSettingMapper.getScanDayCount(paramsMap);

        paramsMap.put("startDays",2);
        paramsMap.put("endDays",1);
        //2日前扫描次数
        Map<String,Long> twoDaysMap = tbParamSettingMapper.getScanDayCount(paramsMap);

        paramsMap.put("startDays",1);
        paramsMap.put("endDays",0);
        //1日前扫描次数
        Map<String,Long> firstDaysMap = tbParamSettingMapper.getScanDayCount(paramsMap);

        //近5日扫描总数new
        paramsMap.put("startDays",5);
        paramsMap.put("endDays",0);
        Map<String,Long> sumScanCount = tbParamSettingMapper.getScanDayCount(paramsMap);

        Map<String,Long> resultMap = new HashMap<>();

        resultMap.put("fiveDaysMap",  fiveDaysMap.get("number"));
        resultMap.put("fourDaysMap",  fourDaysMap.get("number"));
        resultMap.put("threeDaysMap", threeDaysMap.get("number"));

        resultMap.put("twoDaysMap",   twoDaysMap.get("number"));
        resultMap.put("firstDaysMap", firstDaysMap.get("number"));
        resultMap.put("sumScanCount",Long.valueOf(sumScanCount.get("number")));

        return resultMap;
    }

    /**
 * 获取自动登录天数
 * @return
 */
@Override
public Map<String, Long> getAutoLoginDayCount() {
    Map<String,Integer>  paramsMap = new HashMap<>();
    paramsMap.put("startDays",5);
    paramsMap.put("endDays",4);
    //5日前扫描次数
    Map<String,Long> fiveDaysMap = tbParamSettingMapper.getAutoLoginDayCount(paramsMap);
    paramsMap.put("startDays",4);
    paramsMap.put("endDays",3);
    //4日前扫描次数
    Map<String,Long> fourDaysMap = tbParamSettingMapper.getAutoLoginDayCount(paramsMap);

    paramsMap.put("startDays",3);
    paramsMap.put("endDays",2);
    //3日前扫描次数
    Map<String,Long> threeDaysMap = tbParamSettingMapper.getAutoLoginDayCount(paramsMap);

    paramsMap.put("startDays",2);
    paramsMap.put("endDays",1);
    //2日前扫描次数
    Map<String,Long> twoDaysMap = tbParamSettingMapper.getAutoLoginDayCount(paramsMap);

    paramsMap.put("startDays",1);
    paramsMap.put("endDays",0);
    //1日前扫描次数
    Map<String,Long> firstDaysMap = tbParamSettingMapper.getAutoLoginDayCount(paramsMap);

    //近5日扫描总数new
    paramsMap.put("startDays",5);
    paramsMap.put("endDays",0);
    Map<String,Long> sumAutoLoginCount = tbParamSettingMapper.getAutoLoginDayCount(paramsMap);

    Map<String,Long> resultMap = new HashMap<>();

    resultMap.put("fiveDaysMap",  fiveDaysMap.get("number"));
    resultMap.put("fourDaysMap",  fourDaysMap.get("number"));
    resultMap.put("threeDaysMap", threeDaysMap.get("number"));

    resultMap.put("twoDaysMap",   twoDaysMap.get("number"));
    resultMap.put("firstDaysMap", firstDaysMap.get("number"));
    resultMap.put("sumAutoLoginCount",Long.valueOf(sumAutoLoginCount.get("number")));

    return resultMap;
}



    /**
     * 获取半自动操作天数
     * @return
     */
    @Override
    public Map<String, Long> getSemiAutoOperationDayCount() {
        Map<String,Integer>  paramsMap = new HashMap<>();
        paramsMap.put("startDays",5);
        paramsMap.put("endDays",4);
        //5日前扫描次数
        Map<String,Long> fiveDaysMap = tbParamSettingMapper.getSemiAutoOperationDayCount(paramsMap);
        paramsMap.put("startDays",4);
        paramsMap.put("endDays",3);
        //4日前扫描次数
        Map<String,Long> fourDaysMap = tbParamSettingMapper.getSemiAutoOperationDayCount(paramsMap);

        paramsMap.put("startDays",3);
        paramsMap.put("endDays",2);
        //3日前扫描次数
        Map<String,Long> threeDaysMap = tbParamSettingMapper.getSemiAutoOperationDayCount(paramsMap);

        paramsMap.put("startDays",2);
        paramsMap.put("endDays",1);
        //2日前扫描次数
        Map<String,Long> twoDaysMap = tbParamSettingMapper.getSemiAutoOperationDayCount(paramsMap);

        paramsMap.put("startDays",1);
        paramsMap.put("endDays",0);
        //1日前扫描次数
        Map<String,Long> firstDaysMap = tbParamSettingMapper.getSemiAutoOperationDayCount(paramsMap);

        //近5日扫描总数new
        paramsMap.put("startDays",5);
        paramsMap.put("endDays",0);
        Map<String,Long> sumSemiAutoOperationCount = tbParamSettingMapper.getSemiAutoOperationDayCount(paramsMap);

        Map<String,Long> resultMap = new HashMap<>();

        resultMap.put("fiveDaysMap",  fiveDaysMap.get("number"));
        resultMap.put("fourDaysMap",  fourDaysMap.get("number"));
        resultMap.put("threeDaysMap", threeDaysMap.get("number"));

        resultMap.put("twoDaysMap",   twoDaysMap.get("number"));
        resultMap.put("firstDaysMap", firstDaysMap.get("number"));
        resultMap.put("sumSemiAutoOperationCount",Long.valueOf(sumSemiAutoOperationCount.get("number")));

        return resultMap;
    }

    private Map<String,Integer> getGrid(){
        Map<String,Integer> map = new HashMap<>();
        for (int  x= 1 ; x < 21; x++){
            for (int y = 1; y < 21; y++){
                //第几个单元格，初始值设为0；
                map.put("["+(x)+","+(y)+"]",0);
            }
        }
        System.out.println("map>>>"+map.keySet().size());
        return map;
    }

}
